<?php
    include ("../lib/BLL/DmHeDaoTaoBLL.php");
    $ma="" ;
    $ten="";
    $vt="";
    $tt="";
    $tb="";
    $btnName="Thêm";
    $hdt_bll=new DmHeDaoTaoBLL();
    $ds_hdt=array();
        
    if (isset($_POST['timkiem'])) {
        $keysearch="";
		$where="";
        if(isset($_POST['txtkey'])){
            $keysearch=$_POST['txtkey'];
            $where="WHERE".$hdt_bll->buildSearchQueryLike($keysearch);
        }
    }
    
    if(!isset($_POST['timkiem'])||($_POST['txtkey']=="")){
        $where="WHERE 1=1";
    }
    if(isset($_GET['ma'])){
        $btnName="Sửa";
        $madm_hdt=$_GET['ma'];
        $kq=$hdt_bll->getItemHDT($madm_hdt);
        $ma=$kq['madm_hdt'] ;
        $ten=$kq['ten_hedt'] ;
        $vt=$kq['ten_vt_hedt'] ;
        $tt="readonly";
    }
    if(isset($_POST['btnLuu'])){
        $hdt= new DmHeDaoTaoDAL();
        $madm_hdt=$_POST['madm_hdt'];
        $ten_hedt=$_POST['ten_hedt'];
        $ten_vt_hedt=$_POST['ten_vt_hedt'];
        if($btnName=="Thêm"){
            $hdt->setDmHeDaoTaoDAL($madm_hdt,$ten_hedt,$ten_vt_hedt);
            $kq=$hdt_bll->insertHeDaoTao($hdt);
            if($kq==true){
                header("location: chuongtrinhdaotao-hedaotao");
            }
        }else{
            $hdt->setDmHeDaoTaoDAL($madm_hdt,$ten_hedt,$ten_vt_hedt);
            $kq=$hdt_bll->updateHeDaoTao($hdt);
            if ($kq == true){
                header("location: chuongtrinhdaotao-hedaotao");
            }
        }
    }
    if(isset($_GET['xoama'])){
        $maxoa=$_GET['xoama'];
        $kq=$hdt_bll->deleteHeDaoTao($maxoa);
        if ($kq == true){
            header("location: chuongtrinhdaotao-hedaotao");
        }
    }
    if(isset($_POST['xoachon'])){
        if(isset($_POST['checkall'])){
            $kq=$hdt_bll->deleteAll();
            if ($kq == true){
                header("location: chuongtrinhdaotao-hedaotao");
            }
        }
        if(isset($_POST['checkbox'])){
            $delete=$_POST['checkbox'];
            foreach($delete as $madm_nganh){
                $kq=$hdt_bll->deleteHeDaoTao($madm_nganh);
                if ($kq == true){
                    header("location: chuongtrinhdaotao-hedaotao");
                }
            }
        }
    }

    $ds_hdt=$hdt_bll->timkiem($where);
    $totalRecords=count($ds_hdt);        
    $p=(isset($_GET['p']))?$_GET['p']:1;
    $limit=35;                       
    $offset=($p-1)*$limit;
    $ds_hedt=$hdt_bll->getPages($where,$offset,$limit);
    $url="chuongtrinhdaotao-hedaotao";
    $pages=$hdt_bll->paging($totalRecords,$limit,$url,'pagers');

    //$ds_hdt=$hdt_bll->getListHeDaoTao();
    $smarty->assign('p',$p);
    $smarty->assign('pages',$pages);
    $smarty->assign('tt',$tt);
    $smarty->assign('tb',$tb);
    $smarty->assign('ma',$ma);
    $smarty->assign('ten',$ten);
    $smarty->assign('vt',$vt);
    $smarty->assign('btnName',$btnName);
    $smarty->assign('ds_hdt',$ds_hdt);
    $smarty->display('chuongtrinhdaotao/hedaotao.tpl');
?>